package com.redoor.app.stake.basicdata.controller;

import com.redoor.app.stake.auth.service.BaseUserService;
import com.redoor.app.stake.basicdata.commons.BasicDataStatus;
import com.redoor.app.stake.basicdata.model.BaseParkingArea;
import com.redoor.app.stake.basicdata.model.BaseStation;
import com.redoor.app.stake.basicdata.model.BussinessTypeEnum;
import com.redoor.app.stake.basicdata.model.controllerModel.BaseStationCriteria;
import com.redoor.app.stake.basicdata.model.controllerModel.BaseStationModifyBean;
import com.redoor.app.stake.basicdata.model.serviceModel.BaseStationServiceBean;
import com.redoor.app.stake.basicdata.service.BaseParkingAreaService;
import com.redoor.app.stake.basicdata.service.BaseStationService;
import com.redoor.app.stake.basicdata.service.SyncOrganizationService;
import com.redoor.app.stake.common.controller.BaseController;
import com.redoor.app.stake.common.model.Page;
import com.redoor.app.stake.common.utils.JsonUtils;
import com.redoornetwork.framework.core.response.ResponseResult;
import com.redoornetwork.framework.core.response.ResultGenerator;
import com.yotouch.app.stake.model.Station;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* Created by wangjunlei on 2018-02-12 10:08:10.
*/
@RestController
@RequestMapping("/base/stations")
public class BaseStationController extends BaseController {

    @Autowired
    private BaseStationService baseStationInfoService;

    @Autowired
    private SyncOrganizationService syncOrganizationService;
    @Autowired
    private BaseUserService baseUserService;

    @ApiOperation(value = "充电站添加")
    @RequiresPermissions("base:station:insert")
    @PostMapping("/insert")
    public ResponseResult add(@RequestBody BaseStationModifyBean baseStationInfo) {
        baseStationInfoService.modifyStationAndParkingArea(baseStationInfo, BussinessTypeEnum.TRACE_TYPE.WEB);
        return ResultGenerator.generatorSuccess();
    }
    /**
     * @update zhangYiHang
     * @Date 2018/4/8 17:26
     * 修改路径
     */
    @ApiOperation(value = "充电站删除. 返回状态0:未删除,1:删除成功,2:站已绑定桩(站未删除)")
    @ApiImplicitParam(name = "id",value = "主键",paramType = "query",dataType = "String",required = true,defaultValue = "")
    @RequiresPermissions("base:station:delete")
    @PostMapping("/del")
    public ResponseResult delete(String uuid) {
        int status = baseStationInfoService.deleteStation(uuid,BussinessTypeEnum.TRACE_TYPE.WEB);
        return ResultGenerator.generatorSuccess(status);
    }
    @ApiOperation(value = "充电站修改")
    @RequiresPermissions("base:station:update")
    @PostMapping("/update")
    public ResponseResult update(@RequestBody BaseStationModifyBean baseStationInfo) {
        baseStationInfoService.modifyStationAndParkingArea(baseStationInfo, BussinessTypeEnum.TRACE_TYPE.WEB);
        return ResultGenerator.generatorSuccess();
    }

    @GetMapping("/{id}")
    public ResponseResult detail(@PathVariable String id) {
        BaseStation bean = baseStationInfoService.findById(id);
        if(bean != null){
            BaseStationServiceBean baseStationInfo = JsonUtils.readValue(JsonUtils.toJson(bean),BaseStationServiceBean.class);
            if(StringUtils.isEmpty(baseStationInfo.getPartorgcode())){
                baseStationInfo.setPartOrgName(syncOrganizationService.getFullOrgName(baseStationInfo.getCityorgcode()));
            } else {
                baseStationInfo.setPartOrgName(syncOrganizationService.getFullOrgName(baseStationInfo.getPartorgcode()));
            }

            baseStationInfo.setPrOrgName(syncOrganizationService.getFullOrgName(baseStationInfo.getProrgcode()));
            baseStationInfo.setManagerOrgName(syncOrganizationService.getFullOrgName(baseStationInfo.getManagerorgcode()));
            baseStationInfo.setMonitorOrgName(syncOrganizationService.getFullOrgName(baseStationInfo.getMonitororgcode()));
            baseStationInfo.setDistrictName(syncOrganizationService.getDistrictName(baseStationInfo.getDistrictcode()));
            baseStationInfo.setCityName(syncOrganizationService.getDistrictName(baseStationInfo.getCitycode()));
            baseStationInfo.setProvinceName(syncOrganizationService.getDistrictName(baseStationInfo.getProvincecode()));
            return ResultGenerator.generatorSuccess(baseStationInfo);
        } else {
            return ResultGenerator.generatorFail(BasicDataStatus.STATION_IS_NOTEXISTS);
        }
    }


    @ApiOperation(value = "根据条件分页查询站点信息", notes = "根据条件分页查询站点信息")
    @RequiresPermissions("base:station:list")
    @PostMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "limit", value = "偏移量", paramType = "query", dataType = "int", required = true, defaultValue = "20"),
            @ApiImplicitParam(name = "offset", value = "起始记录", paramType = "query", dataType = "int", required = true, defaultValue = "1"),
            @ApiImplicitParam(name = "orgCode", value = "资产机构", paramType = "query", dataType = "String", required = false, defaultValue = ""),
            @ApiImplicitParam(name = "managerorgCode", value = "资产运维机构", paramType = "query", dataType = "String", required = false, defaultValue = ""),
            @ApiImplicitParam(name = "monitororgCode", value = "资产监控机构", paramType = "query", dataType = "String", required = false, defaultValue = ""),
            @ApiImplicitParam(name = "prorgCode", value = "资产产权机构", paramType = "query", dataType = "String", required = false, defaultValue = "")
    })
    public ResponseResult list(BaseStationCriteria param) {
        List<String> userCode = this.baseUserService.districtPermission();
        param.setUncode(userCode);
        int total = baseStationInfoService.qryStationInfoCnt(param);
        if(total > 10000) {
            if(param.getLimit() < 0 ){
                param.setLimit(100);
            }
        }
        List<BaseStationServiceBean> list = baseStationInfoService.qryStationServiceInfoDetail(param);
        for (BaseStationServiceBean bean:list) {
            bean.setPrOrgName(syncOrganizationService.getFullOrgName(bean.getProrgcode()));
            bean.setManagerOrgName(syncOrganizationService.getFullOrgName(bean.getManagerorgcode()));
            bean.setMonitorOrgName(syncOrganizationService.getFullOrgName(bean.getMonitororgcode()));
        }

        Page pageInfo = new Page();
        pageInfo.setTotal(total);
        pageInfo.setData(list);
        return ResultGenerator.generatorSuccess(pageInfo);
    }

    @ApiOperation(value = "根据条件查询站点数目", notes = "根据条件查询站点数目")
    @PostMapping("/total")
        public ResponseResult count(BaseStationCriteria param) {
        List<String> userCode = this.baseUserService.districtPermission();
        param.setUncode(userCode);
        int total = baseStationInfoService.qryStationInfoCnt(param);
        return ResultGenerator.generatorSuccess(total);
    }


    @ApiOperation(value = "自定义条件分页查询站点信息", notes = "根据条件分页查询站点信息")
    @PostMapping("/listForHsf")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "limit", value = "偏移量", paramType = "query", dataType = "int", required = true, defaultValue = "20"),
            @ApiImplicitParam(name = "offset", value = "起始记录", paramType = "query", dataType = "int", required = true, defaultValue = "1")
    })
    public ResponseResult listForHsf(BaseStationCriteria param) {
        List<Station> list = baseStationInfoService.qryStationInfoDetailForHSF(param);
        int total = baseStationInfoService.qryStationInfoCnt(param);
        Page pageInfo = new Page();
        pageInfo.setTotal(total);
        pageInfo.setData(list);
        return ResultGenerator.generatorSuccess(pageInfo);
    }

    @ApiOperation(value = "查询站点下停车区列表", notes = "")
    @PostMapping("/qryParkingAreaListForStation")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uuid", value = "站uuid", paramType = "query", dataType = "String", required = true, defaultValue = "")
    })
    public ResponseResult qryParkingAreaListForStation(String uuid) {
        List<BaseParkingArea> parkingAreaList = baseStationInfoService.qryParkingAreaListForStationUuid(uuid);
        return ResultGenerator.generatorSuccess(parkingAreaList);
    }

    /**
     * 根据站名称验证数据是否存在,0为没有,其他为查找数量
     * @Author zhangYiHang
     * @Date 2018/4/12 20:05
     * @Param
     */
    @ApiOperation(value = "根据站名称验证数据是否存在")
    @ApiImplicitParam(name = "name", value = "站名称", paramType = "query", dataType = "String", required = true, defaultValue = "")
    @PostMapping("/qryStationCountByName")
    public ResponseResult qryStationCountByName(String name){
        int count = this.baseStationInfoService.qryStationCountByName(name);
        return ResultGenerator.generatorSuccess(count);
    }
}
